package cn.com.cifi.mars.common.base;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

public class BaseServiceImpl<T> implements BaseService<T> {

	protected BaseMapper<T> mapper;

	public void setMapper(BaseMapper<T> mapper) {
		this.mapper = mapper;
	}

	@Override
	@Transactional(readOnly=true)
	public T getByPrimaryKey(final Long id) throws Exception {
		return mapper.getByPrimaryKey(id);
	}

	@Override
	@Transactional(readOnly=true)
	public T getByMapParams(Map<String, Object> params) throws Exception {
		return mapper.getByMapParams(params);
	}

	@Override
	@Transactional(readOnly=true)
	public List<T> getListByMapParams(Map<String,Object> params) throws Exception {
		return mapper.getListByMapParams(params);
	}

	@Override
	@Transactional(readOnly=true)
	public PageInfo<T> getListByPage(Map<String, Object> params, Integer pageNo, Integer pageSize) throws Exception {
		if (pageNo == null || pageSize == null) {
			return null;
		}
		Page<T> page = PageHelper.startPage(pageNo, pageSize,true);
		PageInfo<T> pageInfo = new PageInfo<T>(mapper.getListByPage(params));
		pageInfo.setTotal(page.getTotal());
		return pageInfo;
	}

	@Override
	@Transactional
	public void insert(final T entity) throws Exception
	{
		mapper.insert(entity);
	}

	@Override
	@Transactional
	public void insertBatch(List<T> list) throws Exception
	{
		mapper.insertBatch(list);
	}

	@Override
	@Transactional
	public void updateByPrimaryKey(final Long id) throws Exception
	{
		mapper.updateByPrimaryKey(id);
	}

	@Override
	@Transactional
	public void update(final T entity) throws Exception
	{
		mapper.update(entity);
	}

	@Override
	@Transactional
	public void updateBatch(List<T> list) throws Exception
	{
		mapper.updateBatch(list);
	}

	@Override
	@Transactional
	public void deleteByPrimaryKey(final Long id) throws Exception {
		mapper.deleteByPrimaryKey(id);
	}

	@Override
	@Transactional
	public void delete(final T entity) throws Exception {
		mapper.delete(entity);
	}

	@Override
	@Transactional
	public void deleteBatch(List<T> list) throws Exception {
		mapper.deleteBatch(list);
	}
	
}
